package org.tloss.web.editor.utils;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.TreeColumnLayout;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;

public class VariableBrowser extends Dialog {

	CheckboxTreeViewer treeViewer;
	List<Object> supportedVariable;

	public void setSupportedVariable(List<Object> supportedVariable) {

		this.supportedVariable = supportedVariable;
	}

	public VariableBrowser(Shell parentShell) {
		super(parentShell);
		setShellStyle(getShellStyle() | SWT.MAX | SWT.RESIZE);
	}

	@Override
	protected void okPressed() {
		Object[] objects = treeViewer.getCheckedElements();
		if (objects != null) {
			if (objects.length > 0) {
				for (int i = 0; i < objects.length; i++) {
					variable.add((Variable) objects[i]);
				}
			} else {
				variable.clear();
			}
		} else {
			variable.clear();
		}
		super.okPressed();
	}

	protected Control createDialogArea(Composite parent) {
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessVerticalSpace = true;
		Composite composite = new Composite(parent, SWT.FILL | SWT.BORDER);
		composite.setLayoutData(gridData);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		layout.numColumns = 2;

		Composite treeContainer = new Composite(composite, SWT.BORDER);
		gridData = new GridData();
		treeContainer.setLayoutData(gridData);
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessVerticalSpace = true;
		TreeColumnLayout treeLayout = new TreeColumnLayout();
		treeContainer.setLayout(treeLayout);
		treeViewer = new CheckboxTreeViewer(treeContainer, SWT.MULTI
				| SWT.FULL_SELECTION);
		Tree tree = treeViewer.getTree();
		tree.setHeaderVisible(true);
		treeViewer.setContentProvider(new VariableListProvider());

		TreeColumn valueColumn;
		valueColumn = new TreeColumn(tree, SWT.NONE);
		valueColumn.setText("Variable");
		treeLayout.setColumnData(valueColumn, new ColumnWeightData(6));
		TreeViewerColumn column2 = new TreeViewerColumn(treeViewer, valueColumn);
		column2.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				if (element != null) {
					return (element).toString();
				}
				return "";
			}

			public Image getImage(Object element) {

				return Constants.VARIABLE_IMAGE;
			}
		});
		treeViewer.setInput(supportedVariable);

		treeViewer.setAutoExpandLevel(TreeViewer.ALL_LEVELS);

		return composite;
	}

	List<Variable> variable = new ArrayList<Variable>();

	public List<Variable> getVariable() {
		return variable;
	}
}
